package com.zenithmind.mall.pojo.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.zenithmind.common.base.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 订单收货信息实体 - 遵循单一职责原则，专门管理收货信息
 * 
 * @author ZenithMind Team
 * @since 2025-07-03
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("mall_order_receiver")
public class OrderReceiver extends BaseEntity {

    /**
     * 订单ID
     */
    private String orderId;

    /**
     * 收货人姓名
     */
    private String receiverName;

    /**
     * 收货人电话
     */
    private String receiverPhone;

    /**
     * 收货地址
     */
    private String receiverAddress;

    /**
     * 收货地址详情（省市区）
     */
    private String receiverAddressDetail;

    /**
     * 邮政编码
     */
    private String receiverZip;

    /**
     * 省份
     */
    private String province;

    /**
     * 城市
     */
    private String city;

    /**
     * 区县
     */
    private String district;

    /**
     * 详细地址
     */
    private String detailAddress;

    // 业务方法

    /**
     * 获取完整地址
     */
    public String getFullAddress() {
        StringBuilder address = new StringBuilder();
        
        if (province != null && !province.trim().isEmpty()) {
            address.append(province);
        }
        if (city != null && !city.trim().isEmpty()) {
            address.append(city);
        }
        if (district != null && !district.trim().isEmpty()) {
            address.append(district);
        }
        if (detailAddress != null && !detailAddress.trim().isEmpty()) {
            address.append(detailAddress);
        }
        
        return address.toString();
    }

    /**
     * 检查收货人信息是否完整
     */
    public boolean isComplete() {
        return receiverName != null && !receiverName.trim().isEmpty() &&
               receiverPhone != null && !receiverPhone.trim().isEmpty() &&
               hasValidAddress();
    }

    /**
     * 检查地址信息是否有效
     */
    public boolean hasValidAddress() {
        return (receiverAddress != null && !receiverAddress.trim().isEmpty()) ||
               (province != null && !province.trim().isEmpty() &&
                city != null && !city.trim().isEmpty() &&
                detailAddress != null && !detailAddress.trim().isEmpty());
    }

    /**
     * 检查电话号码格式是否有效
     */
    public boolean hasValidPhone() {
        if (receiverPhone == null || receiverPhone.trim().isEmpty()) {
            return false;
        }
        
        // 简单的手机号码验证（11位数字）
        String phone = receiverPhone.replaceAll("[^0-9]", "");
        return phone.length() == 11 && phone.startsWith("1");
    }

    /**
     * 检查邮政编码格式是否有效
     */
    public boolean hasValidZipCode() {
        if (receiverZip == null || receiverZip.trim().isEmpty()) {
            return true; // 邮政编码可以为空
        }
        
        // 中国邮政编码格式验证（6位数字）
        String zip = receiverZip.replaceAll("[^0-9]", "");
        return zip.length() == 6;
    }

    /**
     * 格式化电话号码
     */
    public String getFormattedPhone() {
        if (receiverPhone == null || receiverPhone.trim().isEmpty()) {
            return "";
        }
        
        String phone = receiverPhone.replaceAll("[^0-9]", "");
        if (phone.length() == 11) {
            return phone.substring(0, 3) + "****" + phone.substring(7);
        }
        
        return receiverPhone;
    }

    /**
     * 从用户地址信息复制
     */
    public void copyFromUserAddress(UserAddress userAddress) {
        if (userAddress != null) {
            this.receiverName = userAddress.getReceiverName();
            this.receiverPhone = userAddress.getReceiverPhone();
            this.province = userAddress.getProvince();
            this.city = userAddress.getCity();
            this.district = userAddress.getDistrict();
            this.detailAddress = userAddress.getDetailAddress();
            this.receiverZip = userAddress.getZipCode();
            
            // 生成完整地址
            this.receiverAddress = this.detailAddress;
            this.receiverAddressDetail = (this.province != null ? this.province : "") +
                                       (this.city != null ? this.city : "") +
                                       (this.district != null ? this.district : "");
        }
    }

    /**
     * 验证收货信息数据完整性
     */
    public boolean isValid() {
        return orderId != null && !orderId.trim().isEmpty() &&
               isComplete() &&
               hasValidPhone() &&
               hasValidZipCode();
    }

    /**
     * 获取地址摘要（用于显示）
     */
    public String getAddressSummary() {
        String fullAddress = getFullAddress();
        if (fullAddress.length() > 30) {
            return fullAddress.substring(0, 27) + "...";
        }
        return fullAddress;
    }
}
